//
//  ViewController.m
//  搜索栏
//
//  Created by caimengjun on 16/12/12.
//  Copyright © 2016年 caimengjun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate,UITableViewDataSource,UITextViewDelegate>
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) UITableView *myTableView;


@property (nonatomic,strong) NSMutableArray *dataList;//原始数据
@property (nonatomic,strong) NSMutableArray *searchList;//显示数据

@end

@implementation ViewController

-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray arrayWithCapacity:100];//容量
        for (NSInteger i=0; i<100; i++) {
            [_dataList addObject:[NSString stringWithFormat:@"%ld",(long)i]];
        }
    }
    return _dataList;
}
//要显示在myTableView上面的数组
- (NSMutableArray *)searchList{
    if (!_searchList) {
        _searchList = [NSMutableArray array];
    }
    return _searchList;
}

- (UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] init];
        _myTableView.frame = CGRectMake(0, 70, 377, self.view.bounds.size.height-70);
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

//@synthesize searchBar;

    //创建searchbar
-(UISearchBar *) searchBar{
    
    if (!_searchBar) {
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 50)];
    
    searchBar.barStyle = UIBarStyleDefault;//默认风格 白色搜索框多出的背景为灰色
    searchBar.searchBarStyle = UISearchBarStyleMinimal;//设置整体风格不显示背景
    
    searchBar.text = @"搜索栏";
    searchBar.prompt = @"prompt";
    searchBar.placeholder = @"占位符";
    searchBar.showsBookmarkButton = YES;//在搜索栏右侧是否显示一个图书按钮
    searchBar.showsCancelButton = YES;//是否显示取消按钮
    
    searchBar.tintColor = [UIColor yellowColor];//设置搜索框光标颜色
    searchBar.barTintColor = [UIColor redColor];//设置搜索框背景颜色
    
    searchBar.translucent = NO;//是否半透明
    
    searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(20,0);//调整搜索框field的位置，其他searchbar上面其他控件位置不发生改变
    searchBar.searchTextPositionAdjustment = UIOffsetMake(50, 0);//field里面的文字在field中的位置调整
    
    [searchBar setImage:[UIImage imageNamed:@"1"]
       forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];//自定义搜索框放大镜的图标
    [searchBar setImage:[UIImage imageNamed:@"1"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];//Bookmark图标的设置
    
//self.searchBar.delegate = self;
    [self.view addSubview:_searchBar];
        }
    return _searchBar;
    }



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.searchList count] > 0 ? [self.searchList count] : 0;   }



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    if (self.searchList != nil && self.searchList.count > 0) {
        NSInteger row = [indexPath row];
        cell.textLabel.text = [self.searchList objectAtIndex:row];
    }
    return cell;
}


-(void) viewDidLoad {
    [super viewDidLoad];
    self.searchBar.delegate = self ;
}


//当搜索框将要开始使用时调用。yes表示搜索框可以使用，默认为yes否则搜索框无法使用
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"ShouldBegin");
    return YES;
}
//当搜索框开始编辑时候调用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"DidBegin");
    
}
//当搜索框将要将要结束使用时调用。
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"ShouldEnd");
    
    return YES;
}
//当搜索框结束编辑时候调用
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"DidEnd");
}

//当field里面内容改变时候就开始掉用。
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    if (searchText!=nil && searchText.length>0) {
        _searchList = [NSMutableArray array];//清空searchlist里面的数据
        for (NSString *tempStr in self.dataList)
        {
            if ([tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch].length >0 )
            {
                [self.searchList addObject:tempStr];
                NSLog(@"%ld",[self.searchList count]);
            }
        }
        [self.myTableView reloadData];
    }
    else
    {
        self.searchList = [NSMutableArray arrayWithArray:self.dataList];
        [self.myTableView reloadData];
    }
}
//在field里面输入时掉用，询问是否允许输入，yes表示允许，默认为yes，否则无法输入
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"shouldChange");
    return YES;
}
//点击SearchButton调用
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"SearchButtonClicked");
}
//点击BookmarkButton调用
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"BookmarkButtonClicked");
    
}
//点击CancelButton调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"CancelButton");
}
//点击ResultsListButton调用
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"ResultsListButton");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
